package com.leff.cards.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.leff.cards.R;
import com.leff.cards.util.Secrets;
import com.leff.cards.util.FacebookSession;

public class LoginActivity extends BaseActivity implements Secrets, OnClickListener {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String action = getIntent().getAction();
		
		if(action != null && !action.equals(HomeActivity.ACTION_LOGOUT)) {
			FacebookSession fb = FacebookSession.getSession();
			if(fb.restoreSession(this)) {
				Log.v(TAG, "Facebook credentials restored.");			
				loginComplete();
				return;
			}
		}
		
		this.setContentView(R.layout.login);
		
		Button fbButton = (Button)this.findViewById(R.id.facebook_button);
		fbButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View V) {
		
		if(V.getId() == R.id.facebook_button) {
			FacebookSession.getSession().authorize(this);
		}
	}
	
	public void loginComplete() {
		Intent I = new Intent(this, HomeActivity.class);
		I.setAction("com.leff.cards.GoHomeActivity");
		I.putExtra("doLogin", true);
		
		this.startActivity(I);
		this.finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		FacebookSession.getSession().authorizeCallback(requestCode, resultCode, data);
	}
}
